
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ReadFile2 {

    // {ip, dateStr, method, from}
    private static List<String[]> _inner = new ArrayList<String[]>(0);

    /**
     * 172.22.42.211 - - [31/Jan/2010:20:47:29 -0800] "GET /172.16.76.51/getInfo.htm?from=hello&method=getvalueinfobyvalueid&ids=158;9157;83;350398;350340;8009;5604;365619;100012919;365621; HTTP/1.1" 200 1007 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10" 0
     */
    public static void main(String[] args) throws ParseException {
        File file = new File("/home/brian/Desktop/v_dsl_log.1");
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                if (tempString.contains("/getInfo.htm?")) {
                    doHandle(tempString);
                }
            }
            doPrint();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        file = new File("/home/brian/Desktop/v_dsl_log.2");
        reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                if (tempString.contains("/getInfo.htm?")) {
                    doHandle(tempString);
                }
            }
            doPrint();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    private static void doPrint() throws IOException {
        FileWriter fileWriter = new FileWriter("/home/brian/Desktop/_result");
        for (String[] aResult : _inner) {
            fileWriter.write(aResult[0] + "," + aResult[1] + "," + aResult[2] + "," + aResult[3] + "\n");
        }
        fileWriter.close();
    }

    /**
     * 172.22.42.211 - - [31/Jan/2010:20:47:29 -0800] "GET /172.16.76.51/getInfo.htm?from=hello&method=getvalueinfobyvalueid&ids=158;9157;83;350398;350340;8009;5604;365619;100012919;365621; HTTP/1.1" 200 1007 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10" 0
     */
    private static void doHandle(String aStr) throws ParseException {
        String ip = null;
        String dateStr = null;
        String method = null;
        String from = null;

        // ip
        int postOfIp = aStr.indexOf(" - - ");
        ip = aStr.substring(0, postOfIp);
        aStr = aStr.substring(postOfIp + 5);

        // dateStr
        int postOfLeftBrackets = aStr.indexOf("[");
        int postOfRightBrackets = aStr.indexOf(" -");
        dateStr = aStr.substring(postOfLeftBrackets + 1, postOfRightBrackets);
        SimpleDateFormat rawFormat = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
        Date rawDate = rawFormat.parse(dateStr);
        SimpleDateFormat neatFormat = new SimpleDateFormat("yyyy/MM/dd");
        dateStr = neatFormat.format(rawDate);

        // method
        String methodStr = aStr.substring(aStr.indexOf("method=") + 7);
        int positionOfRightMethod = 0;
        if (methodStr.contains("&")) {
            positionOfRightMethod = methodStr.indexOf("&");
        } else if (methodStr.contains(" ")) {
            positionOfRightMethod = methodStr.indexOf(" ");
        }
        method = methodStr.substring(0, positionOfRightMethod);

        // from
        String fromStr = aStr.substring(aStr.indexOf("from=") + 5);
        int positionOfRightFrom = 0;
        if (fromStr.contains("&")) {
            positionOfRightFrom = fromStr.indexOf("&");
        } else if (fromStr.contains(" ")) {
            positionOfRightFrom = fromStr.indexOf(" ");
        }
        from = fromStr.substring(0, positionOfRightFrom);

//        System.out.println(ip + "_" + dateStr + "_" + method + "_" + from);
        _inner.add(new String[] {ip, dateStr, method, from});
    }

}
